package com.bw.product.server.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bw.common.core.domain.Result;
import com.bw.common.core.utils.DateUtils;
import com.bw.product.common.domain.PmsSpuInfo;
import com.bw.product.common.domain.dto.PmsSkuInfoDTO;
import com.bw.product.common.domain.po.PmsBrandPO;
import com.bw.product.common.domain.po.PmsCategoryPO;
import com.bw.product.common.domain.po.PmsProductAttrValuePO;
import com.bw.product.common.domain.po.PmsSkuInfoPO;
import com.bw.product.common.domain.vo.PmsSkuInfoVO;
import com.bw.product.server.mapper.PmsSpuInfoMapper;
import com.bw.product.server.service.IPmsSpuInfoService;
import com.bw.search.common.EsProductModel;
import com.bw.search.remote.RemoteShelvesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * spu信息Service业务层处理
 * 
 * @author bw
 * @date 2024-12-19
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;
    @Autowired
    private RemoteShelvesService remoteShelvesService;

    /**
     * 查询spu信息
     * 
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     * 
     * @param pmsSpuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu信息
     * 
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu信息
     * 
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu信息
     * 
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     * 
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    /**
     * 上架
     * @param spuId
     */
    @Override
    public void shelves(Long spuId) {

        List<PmsSkuInfoVO> skuInfos = getSkuInfoBySpuId(spuId);

        List<EsProductModel> esProductModelBySpuId = getEsProductModelBySpuId(skuInfos, spuId);

        Result result = remoteShelvesService.productStatusUp(JSON.toJSONString(esProductModelBySpuId));

        if (result.getCode() == 200){
            pmsSpuInfoMapper.updateStatus(1, spuId);
        }
    }

    public List<EsProductModel> getEsProductModelBySpuId(List<PmsSkuInfoVO> skuInfos, Long spuId) {

        List<PmsProductAttrValuePO> pmsProductAttrValuePOS = pmsSpuInfoMapper.selectPmsProductAttrValueList(spuId);
        List<EsProductModel.Attrs> attrsList = pmsProductAttrValuePOS.stream().map(attrValues -> {

            EsProductModel.Attrs attrs = new EsProductModel.Attrs();
            attrs.setAttrId(attrValues.getAttrId());
            attrs.setAttrName(attrValues.getAttrName());
            attrs.setAttrValue(attrValues.getAttrValue());
            return attrs;

        }).collect(Collectors.toList());

        List<EsProductModel> list = skuInfos.stream().map(skuInfo -> {

            EsProductModel esProductModel = new EsProductModel();
            esProductModel.setSkuId(skuInfo.getSkuId());
            esProductModel.setSpuId(skuInfo.getSpuId());
            esProductModel.setSkuTitle(skuInfo.getSkuTitle());
            esProductModel.setSkuPrice(skuInfo.getPrice());
            esProductModel.setSkuImg(skuInfo.getSkuDefaultImg());
            esProductModel.setSaleCount(skuInfo.getSaleCount());
            esProductModel.setHasStock(true);
            esProductModel.setHotScore(Long.valueOf(100));
            esProductModel.setBrandId(skuInfo.getBrandId());
            esProductModel.setCatalogId(skuInfo.getCatalogId());

            PmsBrandPO pmsBrandPOS = pmsSpuInfoMapper.selectPmsBrandList(skuInfo.getBrandId());
            esProductModel.setBrandId(pmsBrandPOS.getBrandId());
            esProductModel.setBrandName(pmsBrandPOS.getName());
            esProductModel.setBrandImg(pmsBrandPOS.getLogo());

            PmsCategoryPO pmsCategoryPO = pmsSpuInfoMapper.selectPmsCategory(skuInfo.getCatalogId());
            esProductModel.setCatalogId(pmsCategoryPO.getCatId());
            esProductModel.setCatalogName(pmsCategoryPO.getName());

            esProductModel.setAttrs(attrsList);

            return esProductModel;

        }).collect(Collectors.toList());

        return list;
    }

    public List<PmsSkuInfoVO> getSkuInfoBySpuId(Long spuId) {

        PmsSkuInfoDTO pmsSkuInfoDTO = new PmsSkuInfoDTO();
        pmsSkuInfoDTO.setSpuId(spuId);
        List<PmsSkuInfoPO> pmsSkuInfoPOS = pmsSpuInfoMapper.selectPmsSkuInfoList(pmsSkuInfoDTO);
        ArrayList<PmsSkuInfoVO> pmsSkuInfoVOS = new ArrayList<>();
        if (pmsSkuInfoPOS != null){
            pmsSkuInfoPOS.forEach(pmsSkuInfoPO -> {
                PmsSkuInfoVO pmsSkuInfoVO = new PmsSkuInfoVO();
                BeanUtils.copyProperties(pmsSkuInfoPO, pmsSkuInfoVO);
                pmsSkuInfoVOS.add(pmsSkuInfoVO);
            });
        }
        return pmsSkuInfoVOS;
    }

}
